	
	
	****************************************************

	* FIGURE 1: FIST STAGE RELATIONSHIP
	 
	****************************************************
	
	* FIGURE 1A : FULL SAMPLE 
	use "$sample", clear
	keep if abs(runvar)<5 //analysis sample
	bysort p25bins: egen mean_tp = mean(tp)
	twoway (qfit tp runvar if runvar<0,clcolor(gs4) lpattern(solid)) ///
	(qfit tp runvar if runvar>=0,clcolor(gs4) lpattern(solid)) ///
	(scatter mean_tp p25bins, msize(medsmall) msymbol(circle) mfcolor(white) mlcolor(black) mlwidth(thin) xline(0, lcolor(grey) lpattern(dash))), ///
	ytitle("Probability of admission", size(medsmall) margin(small)) ylabel(0(0.2)1,labsize(medsmall)  format(%9.1f)) xtitle(Distance to cutoff, size(medsmall) margin(small)) xlabel(, labsize(medsmall)) ///
	graphregion(fcolor(white)) legend(order(2 "Quadratic fit" 3 "0.25 bins")  rows(1)) plotregion(lcolor(black) lwidth(thin)) title("(a) Full Sample")
	graph save "$out/stage1_pooled", replace 
	
	* FIGURE 1B : MALES
	use "$sample", clear
	keep if abs(runvar)<5
	keep if fem==0
	bysort p25bins: egen mean_tp = mean(tp)
	twoway (qfit tp runvar if runvar<0,clcolor(gs4) lpattern(solid)) ///
	(qfit tp runvar if runvar>=0,clcolor(gs4) lpattern(solid)) ///
	(scatter mean_tp p25bins, msize(medsmall) msymbol(circle) mfcolor(white) mlcolor(black) mlwidth(thin) xline(0, lcolor(grey) lpattern(dash))), ///
	ytitle("Probability of admission", size(medsmall) margin(small)) ylabel(0(0.2)1,labsize(medsmall)  format(%9.1f)) xtitle(Distance to cutoff, size(medsmall) margin(small)) xlabel(, labsize(medsmall)) ///
	graphregion(fcolor(white)) legend(order(2 "Quadratic fit" 3 "0.25 bins")  rows(1)) plotregion(lcolor(black) lwidth(thin)) title("(b) Males")
	graph save "$out/stage1_males", replace 
	
	* FIGURE 1C: FEMALES
	use "$sample", clear
	keep if abs(runvar)<5
	keep if fem==1
	bysort p25bins: egen mean_tp = mean(tp)
	twoway (qfit tp runvar if runvar<0,clcolor(gs4) lpattern(solid)) ///
	(qfit tp runvar if runvar>=0,clcolor(gs4) lpattern(solid)) ///
	(scatter mean_tp p25bins, msize(medsmall) msymbol(circle) mfcolor(white) mlcolor(black) mlwidth(thin) xline(0, lcolor(grey) lpattern(dash))), ///
	ytitle("Probability of admission", size(medsmall) margin(small)) ylabel(0(0.2)1,labsize(medsmall)  format(%9.1f)) xtitle(Distance to cutoff, size(medsmall) margin(small)) xlabel(, labsize(medsmall)) ///
	graphregion(fcolor(white)) legend(order(2 "Quadratic fit" 3 "0.25 bins")  rows(1)) plotregion(lcolor(black) lwidth(thin)) title("(c) Females")
	graph save "$out/stage1_females", replace 
	
	
	* COMBINE FIGURES 1A & 1B & 1C INTO ONE GRAPH
	* The following requires the program "grc1leg" by Vince Wiggins
	* This is availble in STATA by typing - findit grc1leg - and then installing
	grc1leg "$out/stage1_pooled.gph" "$out/stage1_males.gph" "$out/stage1_females.gph", scheme(s1mono) rows(1) ycommon xcommon legendfrom("$out/stage1_pooled.gph")
	graph export "$out/Fig1.pdf",   replace //width(6000)
	
	
	
	****************************************************

	* FIGURE 2: COLLEGE QUALITY AND PEERS
	 
	****************************************************
	
	
	use "$sample", clear
	keep if abs(runvar)<5 //analysis sample
	
	*First generate residuals by regressing peer quality on cutoff fixed effect
	
	*Generate for GRADE 12
	reg peermean_12  i.cutoffid, cluster(bin)
	predict u_peermean_12, residuals

	*Generate for GRADE 10
	reg peermean_10  i.cutoffid, cluster(bin)
	predict u_peermean_10, residuals
	
	* NOW GENERATING THE FIGURE
	* FIGURE 2A : GRADE 12
	bysort p25bins: egen mean_peermean_12 = mean(u_peermean_12)
	lpoly u_peermean_12 runvar if runvar<0 , degree(1) kernel(triangle) bwidth(5) /// 
		nograph generate(x_left y_left)	
	lpoly u_peermean_12 runvar if runvar>=0 , degree(1) kernel(triangle) bwidth(5) /// 
		nograph generate(x_right y_right)
	scatter y_left x_left, color(gs8) msize(small) msymbol(circle) || scatter y_right x_right, color(gs8) msize(small)  msymbol(circle) ///
		|| scatter mean_peermean_12 p25bins, msize(medsmall) msymbol(circle) mfcolor(white) mlcolor(black) mlwidth(thin) xline(0, lcolor(grey) lpattern(dash)) ///
		xline(0, lcolor(grey) lpattern(dash)) ///
	ytitle("Average grade 12 score", size(medsmall) margin(small)) ylabel(-4(1)4,labsize(medsmall)  format(%9.0f)) xtitle("Distance to cutoff", size(medsmall) margin(small)) xlabel(, labsize(medsmall)) ///
	graphregion(fcolor(white)) legend(order(1 "Kernel weighted local polynomial fit" 3 "0.25 bins")  rows(1)) title("(a) Average grade 12 score") plotregion(lcolor(black) lwidth(thin)) 
	graph save "$out/C12_quality", replace
	
	
	drop x_left y_left x_right y_right //some cleaning
	
	* FIGURE 2B : GRADE 10
	bysort p25bins: egen mean_peermean_10 = mean(u_peermean_10)
	lpoly u_peermean_10 runvar if runvar<0 , degree(1) kernel(triangle) bwidth(5) /// 
		nograph generate(x_left y_left)	
	lpoly u_peermean_10 runvar if runvar>=0 , degree(1) kernel(triangle) bwidth(5) /// 
		nograph generate(x_right y_right)
	scatter y_left x_left, color(gs8) msize(small) msymbol(circle) || scatter y_right x_right, color(gs8) msize(small) msymbol(circle) ///
		|| scatter mean_peermean_10 p25bins, msize(medsmall) msymbol(circle) mfcolor(white) mlcolor(black) mlwidth(thin) xline(0, lcolor(grey) lpattern(dash)) ///
		xline(0, lcolor(grey) lpattern(dash)) ///
	ytitle("Average grade 10 score", size(medsmall) margin(small)) ylabel(-4(1)4,labsize(medsmall)  format(%9.0f)) xtitle("Distance to cutoff", size(medsmall) margin(small)) xlabel(, labsize(medsmall)) ///
	graphregion(fcolor(white)) legend(order(1 "Kernel weighted local polynomial fit" 3 "0.25 bins")  rows(1)) title("(b) Average grade 10 score") plotregion(lcolor(black) lwidth(thin)) 
	graph save "$out/C10_quality",  replace
	 
	drop x_left y_left x_right y_right //some cleaning
	 
	* COMBINE FIGURES 2A & 2B INTO ONE GRAPH
	* The following requires the program "grc1leg" by Vince Wiggins
	* This is availble in STATA by typing - findit grc1leg - and then installing
	grc1leg "$out/C12_quality.gph" "$out/C10_quality.gph", scheme(s1mono) rows(1) xcommon legendfrom("$out/C12_quality.gph")
	graph export "$out/Fig2.pdf",   replace //width(6000)
	
	
	* declutter the output folder
	erase "$out/stage1_pooled.gph" 
	erase "$out/stage1_males.gph" 
	erase "$out/stage1_females.gph"
	erase "$out/C12_quality.gph"
	erase "$out/C10_quality.gph"
	
